package com.my.zl.console.service.online.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.dao.online.ZlDatabaseSqlMapper;
import com.my.zl.console.dto.online.databaseSql.ReqDatabaseSqlAddDTO;
import com.my.zl.console.dto.online.databaseSql.ReqDatabaseSqlDelDTO;
import com.my.zl.console.dto.online.databaseSql.ReqDatabaseSqlDetailDTO;
import com.my.zl.console.dto.online.databaseSql.ReqDatabaseSqlEditDTO;
import com.my.zl.console.dto.online.databaseSql.ReqDatabaseSqlQueryDTO;
import com.my.zl.console.dto.online.databaseSql.ReqDatabaseSqlStateDTO;
import com.my.zl.console.dto.online.databaseSql.ResDatabaseSqlAddDTO;
import com.my.zl.console.dto.online.databaseSql.ResDatabaseSqlDelDTO;
import com.my.zl.console.dto.online.databaseSql.ResDatabaseSqlDetailDTO;
import com.my.zl.console.dto.online.databaseSql.ResDatabaseSqlEditDTO;
import com.my.zl.console.dto.online.databaseSql.ResDatabaseSqlQueryDTO;
import com.my.zl.console.dto.online.databaseSql.ResDatabaseSqlStateDTO;
import com.my.zl.console.dto.online.databaseSql.ResDatabaseSqlVO2;
import com.my.zl.console.entity.online.ZlDatabaseSql;
import com.my.zl.console.po.online.ZlDatabaseSqlVo;
import com.my.zl.console.service.base.BaseManager;
import com.my.zl.console.service.online.IDatabaseSqlService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import org.apache.commons.lang.StringUtils;

/**
 * 数据库模板
 * @author ZL
 */
@Service
public class DatabaseSqlServiceImpl extends BaseManager implements IDatabaseSqlService {

    @Autowired
    private ZlDatabaseSqlMapper databaseSqlMapper;

    @Override
    public ResDatabaseSqlQueryDTO queryDatabaseSqlList(ReqDatabaseSqlQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResDatabaseSqlQueryDTO resDTO = BeanUtil.copyProperties(page, ResDatabaseSqlQueryDTO.class);
        
        if(!"1".equals(reqInfo.getQuerySourceType()) && StringUtils.isNotBlank(reqInfo.getSqlTitle())){
            reqInfo.setSqlTitle("%"+reqInfo.getSqlTitle()+"%");
        }
        if(!"1".equals(reqInfo.getQuerySourceType()) && StringUtils.isNotBlank(reqInfo.getTableCode())){
            reqInfo.setTableCode("%"+reqInfo.getTableCode()+"%");
        }
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlDatabaseSqlVo> list = databaseSqlMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResDatabaseSqlVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResDatabaseSqlDetailDTO queryDatabaseSqlDetail(ReqDatabaseSqlDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResDatabaseSqlDetailDTO res = new ResDatabaseSqlDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlDatabaseSql view = databaseSqlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResDatabaseSqlAddDTO insertDatabaseSqlInfo(ReqDTO<ReqDatabaseSqlAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDatabaseSqlAddDTO reqData = reqInfo.getReqData();
        ResDatabaseSqlAddDTO res = new ResDatabaseSqlAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlDatabaseSql record = new ZlDatabaseSql();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setSqlId(IdUtil.simpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("sqlId",record.getSqlId());
        //检查主键是否冲突
        if(databaseSqlMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        databaseSqlMapper.insertSelective(record);
        
        ZlDatabaseSql keyInfo = databaseSqlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResDatabaseSqlEditDTO updateDatabaseSqlInfo(ReqDTO<ReqDatabaseSqlEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDatabaseSqlEditDTO reqData = reqInfo.getReqData();
        ResDatabaseSqlEditDTO res = new ResDatabaseSqlEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlDatabaseSql record = new ZlDatabaseSql();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        databaseSqlMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("sqlId",record.getSqlId());
        ZlDatabaseSql keyInfo = databaseSqlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResDatabaseSqlDelDTO deleteDatabaseSqlInfo(ReqDTO<ReqDatabaseSqlDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDatabaseSqlDelDTO reqData = reqInfo.getReqData();
        ResDatabaseSqlDelDTO res = new ResDatabaseSqlDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        databaseSqlMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResDatabaseSqlStateDTO updateDatabaseSqlState(ReqDTO<ReqDatabaseSqlStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDatabaseSqlStateDTO reqData = reqInfo.getReqData();
        ResDatabaseSqlStateDTO res = new ResDatabaseSqlStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlDatabaseSql record = new ZlDatabaseSql();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        databaseSqlMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("sqlId",record.getSqlId());
        ZlDatabaseSql keyInfo = databaseSqlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}